package com.lucky.back.service.impl;

import com.lucky.back.controller.UploadController;
import com.lucky.back.ex.ServiceException;
import com.lucky.back.mapper.AnnouncementMapper;
import com.lucky.back.mapper.BannerMapper;
import com.lucky.back.pojo.dto.AnnouncementAddNewDTO;
import com.lucky.back.pojo.dto.AnnouncementUpdateDTO;
import com.lucky.back.pojo.dto.BannerAddNewDTO;
import com.lucky.back.pojo.entity.Announcement;
import com.lucky.back.pojo.entity.Banner;
import com.lucky.back.pojo.vo.AnnouncementListItemVO;
import com.lucky.back.pojo.vo.AnnouncementStandardVO;
import com.lucky.back.pojo.vo.BannerListItemVO;
import com.lucky.back.pojo.vo.BannerStandardVO;
import com.lucky.back.service.IAnnouncementService;
import com.lucky.back.service.IBannerService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class AnnouncementServiceImpl implements IAnnouncementService {
    @Autowired
    AnnouncementMapper announcementMapper;

    public AnnouncementServiceImpl() {
        log.debug("创建业务对象：AnnouncementServiceImpl");
    }

    @Override
    public void addNew(AnnouncementAddNewDTO announcementAddNewDTO) {
        log.debug("开始处理【添加公告】的业务，参数：{}", announcementAddNewDTO);
        // 从参数announcementAddNewDTO中获取尝试添加的公告名称
        String name = announcementAddNewDTO.getName();
        // 检查此公告名称是否已经存在：调用Mapper对象的countByName()方法，判断结果是否不为0
        int count = announcementMapper.countByName(name);
        if (count != 0) {
            // 是：名称已存在，不允许创建，抛出异常
            String message = "添加相册失败，尝试添加的公告名称已经被占用！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建announcement对象
        Announcement announcement= new Announcement();
        // 将参数DTO中的数据复制到announcement对象中
        BeanUtils.copyProperties(announcementAddNewDTO, announcement);
        log.debug("即将插入公告数据：{}", announcement);
        // 调用Mapper对象的insert()方法，插入公告数据
        announcementMapper.insert(announcement);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除公告】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        AnnouncementStandardVO queryResult = announcementMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：无此id对应的数据，抛出异常
            String message = "删除公告失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将删除公告数据……");
        announcementMapper.deleteById(id);
        log.debug("删除公告，完成！");
    }

    @Override
    public void updateInfoById(Long id, AnnouncementUpdateDTO announcementUpdateDTO) {
        log.debug("开始处理【修改公告详情】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        AnnouncementStandardVO queryResult = announcementMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改公告详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Announcement announcement = new Announcement();

        BeanUtils.copyProperties(announcementUpdateDTO, announcement);
        announcement.setId(id);
        // 修改数据
        log.debug("即将修改数据：{}", announcement);
        int rows = announcementMapper.updateById(announcement);
        if (rows != 1) {
            String message = "修改公告详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public AnnouncementStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询公告详情】的业务");
        AnnouncementStandardVO announcementStandardVO = announcementMapper.getStandardById(id);
        if (announcementStandardVO == null) {
            String message = "获取相册详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return announcementStandardVO;
    }

    @Override
    public List<AnnouncementListItemVO> list() {
        log.debug("开始处理【查询相册列表】的业务");
        return announcementMapper.list();
    }

}
